package optimization;

import theory.vanpelt.bifurcation.BEFindValues;
import static java.lang.Math.*;
import theory.vanpelt.bifurcation.PDistEstimate;

import common.rng.WELL1024;
import common.statistics.Series;

public class NMEmu {
	
	public static double tau = 400000, w = 1.0, dt = 100, N = 18144;

	public static Series emulate(double B, double E, int nSamples, long randSeed)
	{
		Series ZnSeries = new Series();
		WELL1024 well = new WELL1024(randSeed);
		
		for (int iter = 0; iter < nSamples; iter++) 
		{
			int Zn = 1;
			double c1 = w*B*(exp(dt/tau)-1.00);
			double t = 0;

			for (int i = 0; i < N; i++) {
				int off = 0;
				double c2 = exp(-E*log(Zn));
				t += dt;
				double p = exp(-t/tau)*c1*c2;
				for (int k = 0; k < Zn; k++) {
					double X = well.nextValue(); 
					if (X < p) off += 2;
					else off += 1;
				}
				Zn = off;
			}
			ZnSeries.add(Zn);
			
	//		if (iter % 100 == 99) System.out.println(iter+1);
		}
		return ZnSeries;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		double[] x = BEFindValues.find(5.35, 4.41); System.out.println();
		double B = x[0], E = x[1];
		
		Series s1 = NMEmu.emulate(B, E, 300, 80808080L);
		Series s2 = NMEmu.emulate(B, E, 300, 80818080L);
		System.out.println(s1.mean() + ", " + s1.stdev());
		System.out.println(s2.mean() + ", " + s2.stdev());
	}

}
